package com.firstsource.networking.ip.tracker.util;

import java.util.Collection;
import java.util.LinkedList;

public class DeviceConsole {

	private Collection<DeviceSession> sessions;

	public DeviceConsole(String host) {
		sessions = new LinkedList<DeviceSession>() ;
	}

	/**
	 * broadcast messages to all chatters except sender
	 * 
	 * @param sender
	 * @param message
	 */
	public void broadcast(String sender, String message) {
		say(sender, sender + ":" + message);
	}

	private void say(String sender, String message) {
		synchronized (sessions) {
			for (DeviceSession _chatter : sessions)
				if (!_chatter.getSender().equals(sender))
					_chatter.addMessage(message);
		}
	}

	/**
	 * subscribte to the chatroom
	 * 
	 * @param chatter
	 */

	public void subscribe(DeviceSession chatter) {
		synchronized (sessions) {
			sessions.add(chatter);
		}
	}

	/**
	 * unsubsctibe to the chatroom
	 * 
	 * @param chatter
	 */
	public void unsubscribe(DeviceSession chatter) {
		sessions.remove(chatter);
	}
}
